<template>
    <div class="mainBox">
        <div class="imgBox">
            <div class="title">
                <img src="@/assets/logo.png" alt="">
                <span>IZZE后台管理系统</span>
            </div>
        </div>
        <div class="loginBox">
            <div class="loginContext">
                <div class="logintitle">

                </div>
                <span style="font-size: 25px;">登录</span>
                <div class="item">
                    <div class="iconBox">
                        <el-icon :size="20">
                            <User />
                        </el-icon>
                    </div>
                    <div class="inputBox">
                        <input v-model="user.username"></input>
                    </div>
                </div>
                <div class="item">
                    <div class="iconBox">
                        <el-icon :size="20">
                            <Lock />
                        </el-icon>
                    </div>
                    <div class="inputBox">
                        <input v-model="user.password"></input>
                    </div>
                </div>
                <el-button style="width: 200px; margin-top: 10px;" type="primary" @click="toLogin">登录</el-button>
                <div style="display: flex; width: 200px">
                    <router-link :to="{ path: '/register' }" style="font-size: 10px;">还没账号？点我注册</router-link>
                    <router-link style="font-size: 10px; margin-left: auto;">忘记密码(没实现10)</router-link>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup>
import router from '@/router';
import axios from 'axios'
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';

const token = ref(null)
const user = ref({ username: '', password: '' })


async function toLogin() {
    await axios.post('/api/login/login_user', user.value)
        .then(res => {
            if (res.data.code == 200) {
                ElMessage({
                    message: res.data.message,
                    type: 'success'
                })
                token.value = res.data.data;
                localStorage.setItem("token", token.value);
                
            } else {
                ElMessage({
                    message: res.data.message,
                    type: 'error'
                })
            }

        }).catch(err => {
            console.log(err)
            ElMessage({
                message: err,
                type: 'warning'
            })
        })

    await axios.post("/api/login/return_id?token=" + token.value)
        .then(res => {
            user.value = res.data.data
            localStorage.setItem("id", user.value.id)
            if(res.data.code != 401){
                router.push('/main')
            }else{
                ElMessage({
                    message:'系统错误',
                    type:'error'
                })
            }
            
        })
        .catch(err => {
            console.log("error:", err.data)
        })
}


function isLogin(){
    if(localStorage.getItem('token')!=null){
        router.push('/main')
    }
}
onMounted(()=>{
    isLogin()
})
</script>

<style lang="scss" scoped>
.mainBox {
    display: flex;
    background-image: url('@/assets/beijing2.jpg');
    background-size: cover;
    /* 背景图尺寸自适应 */
    background-position: center;
    /* 背景图居中显示 */
    width: 100%;
    /* 宽度 100% */
    height: 100vh;

    .imgBox {
        width: 70%;
        height: 100vh;

        .title {
            display: flex;
            align-items: center;
            font-size: 25px;
            color: #fff;

            img {
                width: 100px;
                height: 100px;
            }

            span {
                margin-left: -20px;
            }
        }
    }

}

.loginBox {
    height: 100vh;
    width: 30%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

    .loginContext {
        box-shadow: 0px 0px 4px 0px #000;
        background-color: #fff;
        width: 400px;
        height: 500px;
        display: flex;
        align-items: center;
        flex-direction: column;

        .item {
            margin-top: 20px;
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .logintitle {
            width: 100%;
            height: auto;

            background-image: url('@/assets/logintitle.jpg');
            background-size: cover;
            /* 背景图尺寸自适应 */
            background-position: center;
            /* 背景图居中显示 */
            width: 100%;
            /* 宽度 100% */
            height: 25vh;
        }
    }


}



.iconBox {
    height: 32px;
    width: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #ddd;
}

.inputBox {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50px;
    width: 165px;

    input {
        height: 30px;
        border: 1px solid #ddd;
        border-radius: 0;
    }
}
</style>